<!DOCTYPE html>
<html lang="zh">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <title>机器学习常用模型及其 sklearn 包 | Dirac Lee</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="robots" content="noodp" />
<meta name="Description" content="Dirac Lee 的博客"><link rel="prev" href="https://diraclee.gitee.io/2020/10/cs229_lecture8/" /><link rel="next" href="https://diraclee.gitee.io/2020/11/svm/" /><link rel="canonical" href="https://diraclee.gitee.io/2020/10/sklearn-%E5%B8%B8%E7%94%A8%E6%A8%A1%E5%9E%8B%E5%8F%82%E6%95%B0/" />
<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico" />
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="manifest" href="/site.webmanifest">
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">
<meta name="msapplication-TileColor" content="#da532c">
<meta name="theme-color" content="#ffffff"><meta property="og:title" content="机器学习常用模型及其 sklearn 包" />
<meta property="og:description" content="" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://diraclee.gitee.io/2020/10/sklearn-%E5%B8%B8%E7%94%A8%E6%A8%A1%E5%9E%8B%E5%8F%82%E6%95%B0/" />
<meta property="article:published_time" content="2020-10-25T00:00:00+00:00" />
<meta property="article:modified_time" content="2020-10-25T00:00:00+00:00" />
<script type="application/ld+json">
    {
        "@context": "http://schema.org",
        "@type": "BlogPosting",
        "headline": "机器学习常用模型及其 sklearn 包",
        "mainEntityOfPage": {
            "@type": "WebPage",
            "@id": "https:\/\/diraclee.gitee.io\/2020\/10\/sklearn-%E5%B8%B8%E7%94%A8%E6%A8%A1%E5%9E%8B%E5%8F%82%E6%95%B0\/"
        },"image": {
                "@type": "ImageObject",
                "url": "https:\/\/diraclee.gitee.io\/logo.png",
                "width":  800 ,
                "height":  600 
            },"genre": "posts","keywords": "机器学习, sklearn","wordcount":  1188 ,
        "url": "https:\/\/diraclee.gitee.io\/2020\/10\/sklearn-%E5%B8%B8%E7%94%A8%E6%A8%A1%E5%9E%8B%E5%8F%82%E6%95%B0\/","datePublished": "2020-10-25T00:00:00\x2b00:00","dateModified": "2020-10-25T00:00:00\x2b00:00","license": "This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.","publisher": {
                "@type": "Organization",
                "name": "xxxx",
                "logo": {
                "@type": "ImageObject",
                "url": "https:\/\/diraclee.gitee.io\/logo.png",
                "width":  127 ,
                "height":  40 
                }
            },"description": ""
    }
    </script><link rel="stylesheet" href="/css/style.min.css"><link rel="stylesheet" href="/css/lib/fontawesome-free/all.min.css"><link rel="stylesheet" href="/css/lib/animate/animate.min.css"></head>
    <body><script>
            window.isDark = (window.localStorage && window.localStorage.getItem('theme')) === 'dark';
            window.isDark && document.body.classList.add('dark-theme');
        </script><div class="wrapper"><nav class="navbar">
    <div class="navbar-container">
        <div class="navbar-header animated bounceIn">
            <a href="https://diraclee.gitee.io">Dirac Lee</a>
        </div>
        <div class="navbar-menu"><a class="menu-item" href="https://diraclee.gitee.io/posts" title="">文章</a><a class="menu-item" href="https://diraclee.gitee.io/tags" title="">标签</a><a class="menu-item" href="https://diraclee.gitee.io/categories" title="">分类</a><a class="menu-item" href="https://diraclee.gitee.io/about" title="">关于</a><a class="menu-item" href="https://diraclee.gitee.io" title="English"><i class="fas fa-language fa-fw"></i></a><a href="javascript:void(0);" class="theme-switch"><i class="fas fa-adjust fa-rotate-180 fa-fw" title="切换主题"></i></a>
        </div>
    </div>
</nav><nav class="navbar-mobile">
    <div class="navbar-container">
        <div class="navbar-header">
            <div class="navbar-header-title animated bounceIn">
                <a href="https://diraclee.gitee.io">Dirac Lee</a>
            </div>
            <div class="menu-toggle" id="menu-toggle">
                <span></span><span></span><span></span>
            </div>
        </div>
        <div class="navbar-menu" id="mobile-menu"><a class="menu-item" href="https://diraclee.gitee.io/posts" title="">文章</a><a class="menu-item" href="https://diraclee.gitee.io/tags" title="">标签</a><a class="menu-item" href="https://diraclee.gitee.io/categories" title="">分类</a><a class="menu-item" href="https://diraclee.gitee.io/about" title="">关于</a><a class="menu-item" href="https://diraclee.gitee.io" title="English"></a><a href="javascript:void(0);" class="theme-switch"><i class="fas fa-adjust fa-rotate-180 fa-fw" title="切换主题"></i></a>
        </div>
    </div>
</nav><main class="main">
                <div class="container"><article class="page"><h1 class="post-title animated flipInX">机器学习常用模型及其 sklearn 包</h1><div class="post-meta">
            <div class="post-meta-main"><a class="author" href="https://diraclee.gitee.io" rel="author" target="_blank">
                    <i class="fas fa-user-circle fa-fw"></i>Dirac Lee
                </a>&nbsp;<span class="post-category">收录于&nbsp;<i class="far fa-folder fa-fw"></i><a href="https://diraclee.gitee.io/categories/%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0/">机器学习</a>&nbsp;</span></div>
            <div class="post-meta-other"><i class="far fa-calendar-alt fa-fw"></i><time datetime=2020-10-25>2020-10-25</time>&nbsp;
                <i class="fas fa-pencil-alt fa-fw"></i>约 1188 字&nbsp;
                <i class="far fa-clock fa-fw"></i>预计阅读 3 分钟&nbsp;</div>
        </div><div class="post-content"><a class="post-dummy-target" id="感知机"></a><h2>感知机</h2>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-python" data-lang="python"><span class="k">class</span> <span class="nc">sklearn</span><span class="o">.</span><span class="n">linear_model</span><span class="o">.</span><span class="n">Perceptron</span><span class="p">(</span>
    <span class="o">*</span><span class="p">,</span> 
    <span class="n">penalty</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> 
    <span class="n">alpha</span><span class="o">=</span><span class="mf">0.0001</span><span class="p">,</span> 
    <span class="n">fit_intercept</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> 
    <span class="n">max_iter</span><span class="o">=</span><span class="mi">1000</span><span class="p">,</span> 
    <span class="n">tol</span><span class="o">=</span><span class="mf">0.001</span><span class="p">,</span> 
    <span class="n">shuffle</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> 
    <span class="n">verbose</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> 
    <span class="n">eta0</span><span class="o">=</span><span class="mf">1.0</span><span class="p">,</span> 
    <span class="n">n_jobs</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> 
    <span class="n">random_state</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> 
    <span class="n">early_stopping</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> 
    <span class="n">validation_fraction</span><span class="o">=</span><span class="mf">0.1</span><span class="p">,</span> 
    <span class="n">n_iter_no_change</span><span class="o">=</span><span class="mi">5</span><span class="p">,</span> 
    <span class="n">class_weight</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> 
    <span class="n">warm_start</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
</code></pre></td></tr></table>
</div>
</div><table>
<thead>
<tr>
<th>参数</th>
<th>范围</th>
<th>含义</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>penalty</code></td>
<td><code>{‘l2’,’l1’,’elasticnet’}</code></td>
<td>所使用的正则项</td>
</tr>
<tr>
<td><code>alpha</code></td>
<td><code>float, default=0.0001</code></td>
<td>正则项权重超参数</td>
</tr>
<tr>
<td><code>fit_intercept</code></td>
<td><code>bool, default=True</code></td>
<td>是否使用截距</td>
</tr>
<tr>
<td><code>max_iter</code></td>
<td><code>int, default=1000</code></td>
<td>最大迭代次数</td>
</tr>
<tr>
<td><code>tol</code></td>
<td><code>float, default=1e-3</code></td>
<td>loss下降多少时终止迭代</td>
</tr>
<tr>
<td><code>shuffle</code></td>
<td><code>bool, default=True</code></td>
<td>是否打乱训练集</td>
</tr>
<tr>
<td><code>verbose</code></td>
<td><code>int, default=0</code></td>
<td>日志详细级别</td>
</tr>
<tr>
<td><code>eta0</code></td>
<td><code>double, default=1</code></td>
<td>更新倍数？</td>
</tr>
<tr>
<td><code>n_jobs</code></td>
<td><code>int, default=None</code></td>
<td>多分类问题中，一对多计算时使用CPU个数，<code>None</code>使用用户在<code>joblib.parallel_backend</code> 中的设置值（默认为<code>1</code>），<code>-1</code> 表示适用所有 CPU</td>
</tr>
<tr>
<td><code>random_state</code></td>
<td><code>int, RandomState instance, default=None</code></td>
<td>打乱训练集所需的随机数或随机状态</td>
</tr>
<tr>
<td><code>early_stopping</code></td>
<td><code>bool, default=False</code></td>
<td>是否当检验分数不再提升时终止训练</td>
</tr>
<tr>
<td><code>validation_fraction</code></td>
<td><code>float, default=0.1</code></td>
<td>用于<code>early_stoping</code> 的检验数据占多大比例</td>
</tr>
<tr>
<td><code>n_iter_no_change</code></td>
<td><code>int, default=5</code></td>
<td>检验分数连续多少轮没有提升时才终止训练</td>
</tr>
<tr>
<td><code>class_weight</code></td>
<td><code>dict</code>,<br />  <code>{class_label: weight} or “balanced”,</code> <code>default=None</code></td>
<td>各个类的权重参数，默认各个类都为<code>1</code>，<br />选择<code>&quot;balance&quot;</code>就是置为<br /> <code>n_samples / (n_classes * np.bicount(y))</code></td>
</tr>
<tr>
<td><code>warm_start</code></td>
<td><code>bool, default=False</code></td>
<td>是否复用上一次的初始模型参数</td>
</tr>
</tbody>
</table>
<a class="post-dummy-target" id="逻辑回归"></a><h2>逻辑回归</h2>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-python" data-lang="python"><span class="k">class</span> <span class="nc">sklearn</span><span class="o">.</span><span class="n">linear_model</span><span class="o">.</span><span class="n">LogisticRegression</span><span class="p">(</span>
    <span class="n">penalty</span><span class="o">=</span><span class="s1">&#39;l2&#39;</span><span class="p">,</span> 
    <span class="o">*</span><span class="p">,</span> 
    <span class="n">dual</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> 
    <span class="n">tol</span><span class="o">=</span><span class="mf">0.0001</span><span class="p">,</span> 
    <span class="n">C</span><span class="o">=</span><span class="mf">1.0</span><span class="p">,</span> 
    <span class="n">fit_intercept</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> 
    <span class="n">intercept_scaling</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> 
    <span class="n">class_weight</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> 
    <span class="n">random_state</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> 
    <span class="n">solver</span><span class="o">=</span><span class="s1">&#39;lbfgs&#39;</span><span class="p">,</span> 
    <span class="n">max_iter</span><span class="o">=</span><span class="mi">100</span><span class="p">,</span> 
    <span class="n">multi_class</span><span class="o">=</span><span class="s1">&#39;auto&#39;</span><span class="p">,</span> 
    <span class="n">verbose</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> 
    <span class="n">warm_start</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> 
    <span class="n">n_jobs</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> 
    <span class="n">l1_ratio</span><span class="o">=</span><span class="bp">None</span><span class="p">)</span>
</code></pre></td></tr></table>
</div>
</div><table>
<thead>
<tr>
<th>参数</th>
<th>范围</th>
<th>含义</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>penalty</code></td>
<td><code>{‘l2’,’l1’,’elasticnet’}</code></td>
<td>所使用的正则项</td>
</tr>
<tr>
<td><code>dual</code></td>
<td><code>bool, default=False</code></td>
<td>是否使用对偶公式？</td>
</tr>
<tr>
<td><code>tol</code></td>
<td><code>float, default=1e-4</code></td>
<td>loss下降多少时终止迭代</td>
</tr>
<tr>
<td><code>C</code></td>
<td><code>float, default=1.0</code></td>
<td>正则项权重的倒数</td>
</tr>
<tr>
<td><code>fit_intercept</code></td>
<td><code>bool, default=True</code></td>
<td>是否使用截距</td>
</tr>
<tr>
<td><code>intercept_scaling</code></td>
<td><code>float, default=1</code></td>
<td>仅当solver选择 <code>linlinear</code> 且 <code>fit_intercept</code> 为 <code>True</code> 时有效，效果？</td>
</tr>
<tr>
<td><code>class_weight</code></td>
<td><code>dict</code>,<br />  <code>{class_label: weight} or “balanced”,</code> <code>default=None</code></td>
<td>各个类的权重参数，默认各个类都为<code>1</code>，<br />选择<code>&quot;balance&quot;</code>就是置为<br /> <code>n_samples / (n_classes * np.bicount(y))</code></td>
</tr>
<tr>
<td><code>random_state</code></td>
<td><code>int, RandomState instance, default=None</code></td>
<td>打乱训练集所需的随机数或随机状态，仅当 solver 选择 <code>&quot;sag&quot;</code>，<code>&quot;saga&quot;</code> 或<code>&quot;liblinear&quot;</code> 时有效</td>
</tr>
<tr>
<td><code>solver</code></td>
<td><code>{‘newton-cg’, ‘lbfgs’, ‘liblinear’, ‘sag’, ‘saga’}, </code> <br /><code>default=’lbfgs’</code></td>
<td>求解器<br />小数据适用于 <code>&quot;liblinear&quot;</code>，大数据 <code>&quot;sag&quot;</code> 和 <code>&quot;saga&quot;</code> 更快<br />对于多分类问题,  <code>&quot;newton-cg&quot;</code>, <code>&quot;sag&quot;</code>, <code>&quot;saga&quot;</code> 和  <code>&quot;lbfgs&quot;</code> 只适用于多项式, <code>liblinear</code> 只适用于<code>ovr</code> <br /><code>‘newton-cg’, ‘lbfgs’, ‘sag’ and ‘saga’</code> 只能用于 无正则或L2正则<br /><code>‘liblinear’ and ‘saga’</code> 可以用于 L1 正则<br /><code>‘saga’</code> 可以用于‘elasticnet’ 惩罚项<br /><code>‘liblinear’</code> 必须设置惩罚项</td>
</tr>
<tr>
<td><code>max_iter</code></td>
<td><code>int, default=100</code></td>
<td>最大迭代次数</td>
</tr>
<tr>
<td><code>multi_class</code></td>
<td><code>{‘auto’, ‘ovr’, ‘multinomial’}, default=’auto’</code></td>
<td>数据是二分类或者选用 <code>&quot;liblinear&quot;</code>求解器时，<code>auto</code> 为 <code>ovr</code>，否则为 <code>multinomial</code></td>
</tr>
<tr>
<td><code>verbose</code></td>
<td><code>int, default=0</code></td>
<td>日志详细级别</td>
</tr>
<tr>
<td><code>warm_start</code></td>
<td><code>bool, default=False</code></td>
<td>是否复用上一次的初始模型参数</td>
</tr>
<tr>
<td><code>n_jobs</code></td>
<td><code>int, default=None</code></td>
<td>多分类问题中，一对多计算时使用CPU个数，<code>None</code>使用用户在<code>joblib.parallel_backend</code> 中的设置值（默认为<code>1</code>），<code>-1</code> 表示适用所有 CPU</td>
</tr>
<tr>
<td><code>l1_ratio</code></td>
<td><code>float, in [0, 1], default=None</code></td>
<td>Elastic-Net的混合参数，取 0 就等价于L2正则，取1就等价于L1正则</td>
</tr>
</tbody>
</table></div><div class="post-footer" id="post-footer">
    <div class="post-info">
        <div class="post-info-line">
            <div class="post-info-mod">
                <span>本文于 2020-10-25 更新</span>
            </div>
            <div class="post-info-license"></div>
        </div>
        <div class="post-info-line">
            <div class="post-info-md"></div>
            <div class="post-info-share"><span><a href="//www.linkedin.com/shareArticle?url=https%3a%2f%2fdiraclee.gitee.io%2f2020%2f10%2fsklearn-%25E5%25B8%25B8%25E7%2594%25A8%25E6%25A8%25A1%25E5%259E%258B%25E5%258F%2582%25E6%2595%25B0%2f&amp;title=%e6%9c%ba%e5%99%a8%e5%ad%a6%e4%b9%a0%e5%b8%b8%e7%94%a8%e6%a8%a1%e5%9e%8b%e5%8f%8a%e5%85%b6%20sklearn%20%e5%8c%85" target="_blank" title="分享到 LinkedIn">
            <i class="fab fa-linkedin fa-fw"></i>
        </a><a href="//service.weibo.com/share/share.php?url=https%3a%2f%2fdiraclee.gitee.io%2f2020%2f10%2fsklearn-%25E5%25B8%25B8%25E7%2594%25A8%25E6%25A8%25A1%25E5%259E%258B%25E5%258F%2582%25E6%2595%25B0%2f&amp;appkey=&amp;title=%e6%9c%ba%e5%99%a8%e5%ad%a6%e4%b9%a0%e5%b8%b8%e7%94%a8%e6%a8%a1%e5%9e%8b%e5%8f%8a%e5%85%b6%20sklearn%20%e5%8c%85" target="_blank" title="分享到 Weibo">
            <i class="fab fa-weibo fa-fw"></i>
        </a></span></div>
        </div>
    </div>

    <div class="post-info-more">
        <section><span class="tag">
                        <a href="https://diraclee.gitee.io/tags/%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0/"><i class="fas fa-tag fa-fw"></i>&nbsp;机器学习</a>&nbsp;
                    </span><span class="tag">
                        <a href="https://diraclee.gitee.io/tags/sklearn/"><i class="fas fa-tag fa-fw"></i>&nbsp;sklearn</a>&nbsp;
                    </span></section>
        <section>
            <span><a href="javascript:window.history.back();">返回</a></span>&nbsp;|&nbsp;<span><a href="https://diraclee.gitee.io">主页</a></span>
        </section>
    </div>

    <div class="post-nav"><a href="https://diraclee.gitee.io/2020/10/cs229_lecture8/" class="prev" rel="prev" title="cs229 第八节"><i class="fas fa-angle-left fa-fw"></i>cs229 第八节</a>
            <a href="https://diraclee.gitee.io/2020/11/svm/" class="next" rel="next" title="SVM 公式推导">SVM 公式推导<i class="fas fa-angle-right fa-fw"></i></a></div>
</div><div class="post-comment"></div>
    </article></div>
            </main><footer class="footer">
    <div class="copyright"><div class="copyright-line">由 <a href="https://gohugo.io/" target="_blank" rel="external nofollow noopener noreffer">Hugo</a> 强力驱动 | 主题 - <a href="https://github.com/dillonzq/LoveIt" target="_blank" rel="external nofollow noopener noreffer">LoveIt<i class="far fa-heart fa-fw"></i></a>
        </div>

        <div class="copyright-line"><i class="far fa-copyright fa-fw"></i><span itemprop="copyrightYear">2020</span><span class="author" itemprop="copyrightHolder">&nbsp;<a href="https://diraclee.gitee.io" target="_blank">Dirac Lee</a></span>&nbsp;|&nbsp;<span class="license"><a rel="license external nofollow noopener noreffer" href="https://creativecommons.org/licenses/by-nc/4.0/" target="_blank">CC BY-NC 4.0</a></span></div>
    </div>
</footer></div><a href="#" class="dynamic-to-top" id="dynamic-to-top" data-scroll>
            <span>&nbsp;</span>
        </a><script src="/js/lib/jquery/jquery.slim.min.js"></script><script src="/js/lib/lazysizes/lazysizes.min.js"></script><script src="/js/lib/smooth-scroll/smooth-scroll.polyfills.min.js"></script><script>window.scroll = new SmoothScroll('[data-scroll]', {speed: 300, speedAsDuration: true});</script><link rel="stylesheet" href="/css/lib/katex/katex.min.css"><script src="/js/lib/katex/katex.min.js"></script><script defer src="/js/lib/katex/auto-render.min.js"></script><link rel="stylesheet" href="/css/lib/katex/copy-tex.min.css"><script defer src="/js/lib/katex/copy-tex.min.js"></script><script defer src="/js/lib/katex/mhchem.min.js"></script><script>
        document.addEventListener("DOMContentLoaded", function () {
            renderMathInElement(document.body, {
                delimiters: [
                    { left: "$$", right: "$$", display: true },
                    { left: "\\(", right: "\\)", display: false },
                    { left: "\\[", right: "\\]", display: true },{ left: "$", right: "$", display: false },]
            });
        });
    </script><script src="/js/blog.min.js"></script></body>
</html>